﻿using AntdUI;
using MiniExcelLibs.Attributes;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImportPlcPointPosition
{
    public class ImportData : AntdUI.NotifyProperty
    {
        public string Guid { get; set; } = System.Guid.NewGuid().ToString("N");

        public ImportData() {
            Operate = new AntdUI.CellLink[] {
                      //  new AntdUI.CellButton("read","读取",AntdUI.TTypeMini.Default),
                        new AntdUI.CellButton("write","写入",AntdUI.TTypeMini.Primary)
                    };
        }

        AntdUI.CellLink[] operate;
        public AntdUI.CellLink[] Operate
        {
            get => operate;
            set
            {
                operate = value;
                OnPropertyChanged("Operate");
            }
        }


        public int Id {  get; set; }
        public int ParentId { get => parentId;
            set {
                parentId = value;
                OnPropertyChanged(nameof(ParentId));
            } }



        public string WriteValue { get; set; } = string.Empty;

        private bool autoWrite = false;
        public bool AutoWrite
        {
            get { return autoWrite; }
            set
            {
                if (autoWrite == value) return;
                autoWrite = value;
                OnPropertyChanged(nameof(AutoWrite));
            }
        }

        public string WatchValue { get => watchValue;
            set 
            {
                watchValue = value;
                OnPropertyChanged("WatchValue");
            }
        }

        public int OrderNo {  get; set; }


        /// <summary>
        /// 描述信息
        /// </summary>
        [ExcelColumnIndex(0)]
        public string Description { get; set; } = string.Empty;
        /// <summary>
        /// 数据类型
        /// </summary>
        [ExcelColumnIndex(1)]
        public string DataType { get; set; } = string.Empty;
        /// <summary>
        /// 偏移量 起始位置
        /// </summary>
        [ExcelColumnIndex(2)]
        public decimal BytePosition { 
            get => bytePosition;
            set
            {
                bytePosition = value;
                OnPropertyChanged(nameof(BytePositionStr));
            }
        }

        public string BytePositionStr
        {
            get => BytePosition.ToString("f1");
           
        }
        /// <summary>
        /// 模块：1上位机 2AGV
        /// </summary>
        public int Module { get; set; }
        public int LocationId { get; set; }
        public int DataId { get; set; }

        public string Remark { get; set; } = string.Empty;
        /// <summary>
        /// 初始值
        /// </summary>
        [ExcelColumnIndex(3)]
        public string DefaultValue {  get; set; }= string.Empty;

        public int BlockDb {  get; set; }

        public int BlockLength { get; set; }

        public string ParseType { get; set; }

        public int ByteLength { get; set; }
        //ByteLenth

        /// <summary>
        /// 作用类型 0 常规数据、1订阅数据、2 MES心跳、3 PLC心跳
        /// </summary>
        public int EffectType {  get; set; }

        /// <summary>
        /// 动作Id，仅订阅数据生效，实例化对应的执行操作类 
        /// </summary>
        public int ActionId { get; set; }

        public string ActionKey { get; set; } = string.Empty;
        /// <summary>
        /// 关联动作Id 
        /// </summary>
        public int LinkActionId { get; set; }
        /// <summary>
        /// 动作下的步骤编号 
        /// </summary>
        public int StepId { get; set; }
        /// <summary>
        /// 数据分类 1 常规读取 2 写入回复信号 ...(可扩展，eg:子篮码、底板码)
        /// 1XXX:读取某种编码 2XXX:写入某种编码
        /// </summary>
        public int DataKind { get; set; } = 1;
        public int PlcId { get; internal set; }
        public string WorkStationCode { get; internal set; }
        bool _check = true;
        private string watchValue =string.Empty;
        private decimal bytePosition;
        private ImportData[] subItems;
        private ImportData[] children;
        private int parentId;

        public bool IsCheck
        {
            get => _check;
            set
            {
                if (_check == value) return;
                _check = value;
                OnPropertyChanged("IsCheck");
            }
        }

        public int WriteFrom { get; set; }

        [SugarColumn(IsIgnore = true)]
        public string WriteFromText
        {
            get
            {



                return WriteFrom switch
                {
                    1 => "PC",
                    2 => "PLC",
                    3 => "混合",
                    _ => ""
                };
            }
        }
        [SugarColumn(IsIgnore = true)]
        public CellText WriteFromCellText
        {
            get
            {
                
                return new CellText(WriteFromText)
                {
                    //支持所有单元格控件
                    //支持所有单元格控件
                    DropDownItems = new ISelectItem[]
                         {
                             new AntdUI.SelectItem("PC",1),
                              new AntdUI.SelectItem("PLC",2),
                               
                         },
                    DropDownValueChanged = (value) =>
                    {
                        int badge = value.ObjToInt();
                        WriteFrom = badge;
                        OnPropertyChanged("WriteFromCellText");
                    }
                };

            }

            //set => writeFromButton = value;
        }


        public string EffectTypeText
        {
            get
            {


                //1订阅数据、2 MES心跳、3 PLC心跳
                return EffectType switch
                {
                    1 => "订阅数据",
                    5 => "数据采集",
                    2 => "MES心跳",
                    3 => "PLC心跳",
                    4 => "实时采集",
                    
                    6 => "订阅+保存结果",
                    7 => "工参确认",
                    8 => "工参上传",
                    9 => "请求加工",
                    _ => "--" // 未定义
                };
            }
        }

        public CellButton EffectTypeCellText
        {
            get
            {
                return new CellButton(this.Id.ToString(),EffectTypeText)
                {
                    //支持所有单元格控件
                    //支持所有单元格控件
                    DropDownItems = new ISelectItem[]
                         {
                            
                             new AntdUI.SelectItem("订阅数据",1),
                              new AntdUI.SelectItem("数据采集",5),
                               new AntdUI.SelectItem("请求加工",9),
                               new AntdUI.SelectItem("工参确认",7),
                               new AntdUI.SelectItem("工参上传",8),
                             new AntdUI.SelectItem("MES心跳",2),
                             new AntdUI.SelectItem("PLC心跳",3),
                              new AntdUI.SelectItem("实时采集",4),
                              new AntdUI.SelectItem("订阅+数据采集",6),
                              new AntdUI.SelectItem("未定义_--",0),
                         },
                    DropDownMaxCount = 9
                    ,
                    DropDownValueChanged = (value) =>
                    {
                        int badge = value.ObjToInt();
                        EffectType = badge;
                        OnPropertyChanged("EffectTypeCellText");
                    }
                };

            }

        }

        public int LastWriteIndex { get; internal set; }
        public ImportData[] SubItems { 
            get => subItems;
            set {
                if (subItems == value) return;
                subItems = value;
                OnPropertyChanged(nameof(SubItems));
            }
        }
        public ImportData[] Children
        {
            get => children;
            set
            {
                if (children == value) return;
                children = value;
                OnPropertyChanged(nameof(Children));
            }
        }
        public int BlockDbId { get;  set; }
        public int TempId { get;  set; }
        public int TempParentId { get;  set; }
    }
}
